/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.wizards.jcproject.pages;

import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipsejcde.core.JCDECore;

/**
 * @author Amgad S Youssef
 *
 */
public class NewJCProjectNameWizardPage extends WizardPage {
	private Text projectNameText;

	private IJavaProject createdJCProject;

	private String projectName;

	public NewJCProjectNameWizardPage() {
		super("org.eclipsejcde.wizards.jcproject.pages.NewJCProjectNameWizardPage");
		setTitle("Java Card Project");
		setDescription("Create a Java Card project in the workspace.");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 10;
		Label label = new Label(container, SWT.NULL);
		label.setText("Project name:");

		projectNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		projectNameText.setLayoutData(gd);
		projectNameText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		setControl(container);
		projectNameText.setFocus();
	}

	public String getProjectName() {
		return projectNameText.getText();
	}

	public void setProjectName(String projectName) {
		this.projectNameText.setText(projectName);
	}

	private void validatePage() {
		String projectName = projectNameText.getText();
		setErrorMessage(null);
		setPageComplete(false);
		if (ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).exists()) {
			setErrorMessage("A project with this name already exists.");
			return;
		}
		if (ResourcesPlugin.getWorkspace().getRoot().getLocation().append(File.separator + projectName).toFile().exists()) {
			setErrorMessage("A directory with this name already exists in the workspace.");
			return;
		}
		setPageComplete(true);
		updateBean();
	}

	public void createJavacardProject(IProgressMonitor monitor) throws CoreException {
		// create normal project
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		iProject.create(monitor);
		iProject.open(monitor);
		ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path("/" + projectName + "/src")).create(true, true, monitor);
		// add java nature to the project
		IProjectDescription iProjectDescription = iProject.getDescription();
		iProjectDescription.setNatureIds(new String[] { JavaCore.NATURE_ID, JCDECore.NATURE_ID });
		iProject.setDescription(iProjectDescription, monitor);
		createdJCProject = JavaCore.create(iProject);
//		Hashtable options = JavaCore.getDefaultOptions();
//		Set set = options.keySet();
//		Iterator it = set.iterator();
//		while(it.hasNext()){
//			Object obj = it.next();
//			System.out.println(obj + " : " + options.get(obj));
//		}
		//org.eclipse.jdt.core.compiler.codegen.targetPlatform : 1.2
		//org.eclipse.jdt.core.compiler.source : 1.3
		//org.eclipse.jdt.core.compiler.compliance : 1.4
		if(JCDECore.getCurrentJCDKVersion().equalsIgnoreCase(JCDECore.SUPPORTED_JCDK_VERSIONS[0])){//221
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.compliance", "1.4");
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.source", "1.3");
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.2");
		}else if(JCDECore.getCurrentJCDKVersion().equalsIgnoreCase(JCDECore.SUPPORTED_JCDK_VERSIONS[1])){//222
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.compliance", "1.5");
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.source", "1.5");
			createdJCProject.setOption("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.5");
		}
		// add java card classpath libraries
		IClasspathEntry[] classpathEntries = new IClasspathEntry[2];
		classpathEntries[0] = JavaCore.newSourceEntry(new Path("/" + projectName + "/src"));
		classpathEntries[1] = JavaCore.newContainerEntry(new Path(JCDECore.JCDK_CONTAINER_PATH));

		createdJCProject.setRawClasspath(classpathEntries, monitor);
		createdJCProject.save(monitor, false);
	}

	private void updateBean() {
		this.projectName = this.projectNameText.getText();
	}

	public IJavaProject getCreatedJCProject() {
		return createdJCProject;
	}
}
